package com.frozenlab.android.weather.adapter;

import java.io.IOException;

import org.w3c.dom.Document;

import android.util.Log;

import com.frozenlab.android.weather.helper.HttpConnectHelper;
import com.frozenlab.android.weather.helper.YahooPlaceFinderHelper;
import com.frozenlab.android.weather.vo.LocationVO;

public class LocationAdapter {
	
	private static final String TAG = "LocationAdapter";

	private HttpConnectHelper httpConnectHelper;
	private String strURL;

	public LocationAdapter() {
		this.httpConnectHelper = new HttpConnectHelper();
	}
	
	public LocationVO getLocationByCoordinate(LocationVO locationVO, String latitude, String longitude) {
		if (latitude == null){
			Log.e(TAG,"latitude is NULL");
			return null;
		}
		if (longitude == null){
			Log.e(TAG,"longitude is NULL");
			return null;
		}
		
		//contrust URL using Yahoo Place Finder 
		strURL = YahooPlaceFinderHelper.createUrl(latitude, longitude);
		Log.e(TAG,"Yahoo URL" +strURL);
		
		try {
			//get document from server
			Log.e(TAG,"get document from server");
			Document doc = httpConnectHelper.getDocumentFromData(strURL);
			
			//parse the document according to Yahoo Place Finder XML format
			if (doc != null){
				Log.e(TAG,"Parsing WOEID from data");
				locationVO = (YahooPlaceFinderHelper.parserYahooPlaceFinderInfo(doc));
			}
		} catch (IOException e) {
			Log.e(TAG,e.toString());
		}
		
		return locationVO;
	}

}
